<template>
  <div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('现金流类型')" prop="type" label-width="120">
        <el-select v-model="queryParams.type" :placeholder="$t('现金流类型')" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发生时间')" prop="operateDates">
        <DatePicker v-model="operateDates" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('方向')" prop="direction">
        <el-select v-model="queryParams.direction" :placeholder="$t('方向')" clearable>
          <el-option
            v-for="dict in directionOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('业务类型')" prop="operateType">
        <el-select v-model="queryParams.operateType" :placeholder="$t('业务类型')" clearable>
          <el-option
            v-for="dict in operateTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:cashflow:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['deal:cashflow:view']"-->
      <!--          type="primary"-->
      <!--          icon="el-icon-info"-->
      <!--          size="mini"-->
      <!--          @click="getIrr"-->
      <!--        >{{ $t("获取IRR") }}</el-button>-->
      <!--      </el-col>-->
    </el-row>
    <el-table v-loading="loading" :data="dealCashflowList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('现金流类型')"
        align="center"
        prop="type"
        :formatter="typeFormat"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('业务类型')"
        align="center"
        prop="operateType"
        :formatter="operateTypeFormat"
        show-overflow-tooltip
      />
      <el-table-column :label="$t('发生时间')" align="center" prop="operateDate" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('发生金额') + $t('金额单位')"
        align="center"
        prop="operateAmount"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.operateAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('方向')"
        align="center"
        prop="direction"
        :formatter="directionFormat"
        show-overflow-tooltip
        width="90"
      />
      <el-table-column
        :label="$t('是否参与IRR计算')"
        align="center"
        prop="whetherIrr"
        :formatter="whetherIrrFormat"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:cashflow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目现金流对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealCashflowForm ref="dealCashflowForm" />
    </el-dialog>
  </div>
</template>

<script>
import { findDealCashFlowList, getDealCashflowIrr } from '@/api/deal/cashflow/dealCashflow'
import dealCashflowForm from '../cashflow/dealCashflowForm'

export default {
  components: {
    dealCashflowForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 项目现金流表格数据
      dealCashflowList: [],
      // 弹出层标题
      title: '现金流',
      // 是否显示弹出层
      formOpen: false,
      // 现金流类型  预计 实际字典
      typeOptions: [],
      // 方向：流入、流出字典
      directionOptions: [],
      // 类型：投资、回款、费用字典
      operateTypeOptions: [],
      // 是否参与IRR计算字典
      whetherIrrOptions: [],
      // 发生时间 参数
      operateDates: [],
      // 查询参数
      queryParams: {
        type: undefined,
        dealId: undefined,
        investorId: this.$route.query.id,
        dataId: undefined,
        component: undefined,
        startDate: undefined,
        endDate: undefined,
        direction: undefined,
        operateType: undefined,
        dealName: undefined,
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getDicts('cash_flow_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('cash_flow_direction').then(response => {
      this.directionOptions = response.data
    })
    this.getDicts('deal_cash_flow_type').then(response => {
      this.operateTypeOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.whetherIrrOptions = response.data
    })
  },
  methods: {
    init(id) {
      this.queryParams.dealId = id
      this.getList()
    },
    /** 查询项目现金流列表 */
    getList() {
      this.loading = true
      if (this.operateDates != null && this.operateDates.length > 1) {
        this.queryParams.startDate = this.operateDates[0]
        this.queryParams.endDate = this.operateDates[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      findDealCashFlowList(this.addDateRange(this.queryParams, this.operateDates)).then(response => {
        this.dealCashflowList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 现金流类型  预计 实际字典翻译
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 方向：流入、流出字典翻译
    directionFormat(row) {
      return this.selectDictLabel(this.directionOptions, row.direction)
    },
    // 类型：投资、回款、费用字典翻译
    operateTypeFormat(row) {
      return this.selectDictLabel(this.operateTypeOptions, row.operateType)
    },
    // 是否参与IRR计算字典翻译
    whetherIrrFormat(row) {
      return this.selectDictLabel(this.whetherIrrOptions, row.whetherIrr)
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.operateDates = []
      this.queryHandle()
    },
    /** 查看按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealCashflowForm.init(row)
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcelPost('/cashflow/dealCashflow/export', this.addDateRange(this.queryParams, this.operateDates))
    },
    // 计算irr
    getIrr() {
      getDealCashflowIrr(this.queryParams).then(res => {
        let msg = ''
        if (res.data === 502) {
          msg = this.$t('计算失败：小于或等于一条现金流')
        } else if (res.data === 503) {
          msg = this.$t('计算失败：第一条现金流的时间不是最早时间')
        } else if (res.data === 504) {
          msg = this.$t('计算失败：第一条现金流的payment的值不为负')
        } else {
          msg = res.data
        }
        this.$alert(msg, '当前数据IRR', {
          dangerouslyUseHTMLString: true
        })
      })
    }
  }
}
</script>
